#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxGuideCurveIndexCheck(CheckBase):
    def __init__(self):
        super(CfxGuideCurveIndexCheck, self).__init__()
        pass

    def check_guide_curve(self):
        # 通过文件名称获取资产信息
        maya_file = mu.sceneName()
        file_name = os.path.basename(maya_file)
        asset_short_type = file_name.split("_")[0]
        asset_name = file_name.split("_")[1]
        print asset_name
        miss_node = []
        curve_group_name = "{0}_{1}_guideCurve_grp".format(asset_short_type, asset_name)
        if not cmds.objExists(curve_group_name):
            miss_node.append(curve_group_name)
            return

        # 检查guideCurve组子节点信息
        check_groups = []
        self.get_check_groups(curve_group_name, check_groups)
        print check_groups
        for curve_group_node in check_groups:
            source_guide_curve_nodes = cmds.listRelatives(curve_group_node, c=True)
            sort_check_nodes = cmds.listRelatives(curve_group_node, c=True)
            sort_check_nodes.sort()
            for index in range(len(source_guide_curve_nodes)):
                source_node = source_guide_curve_nodes[index]
                check_node = sort_check_nodes[index]
                print("source is : ", source_node)
                print("check is : ", check_node)
                if source_node != check_node:
                    miss_node.append(source_node)

        return miss_node

    def get_check_groups(self, group, group_lib=[]):
        for sub_node in cmds.listRelatives(group, c=True):
            if cmds.listRelatives(sub_node, c=True, s=True):
                group_lib.append(group)
                break
            elif cmds.listRelatives(sub_node, c=True):
                self.get_check_groups(sub_node, group_lib)

    def check(self):
        result_proc = self.check_guide_curve()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        pass
